package com.woniuxy.education.mapper;

import com.woniuxy.education.entity.dto.TUInfo;
import com.woniuxy.education.entity.dto.Tclass;
import com.woniuxy.education.entity.Teacher;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author author
 * @create 2021/4/1
 * <p>
 * 教师映射文件
 */

@Mapper
public interface TeacherMapper {
    //查询所有教师
    @Select("select * from teacher")
    Teacher findInfo();

    //查询教师对应的班级,以及班级的对应情况
    @Select("select t.id, t.number,t.name,t.subject,class.cname,class.rank ," +
            "tea_class.t_id,tea_class.c_id " +
            "from teacher t left join tea_class on t.id = tea_class.t_id " +
            "left join class on tea_class.c_id = class.id where t.state !=2")
    List<Tclass> selectTCInfo(@Param("page") int page, @Param("size") int size);

    //查询教师账号信息
    @Select("select t.id,t.number,t.name,t.subject,t.position,t.u_id,u.account,u.password " +
            "from teacher t left join user u on t.u_id = u.id  where t.state = 1")
    List<TUInfo> selectTUInfo(@Param("page") int page, @Param("size") int size);

    //条件查询
    @Select("<script>select t.id, t.number,t.name,t.subject,class.cname,class.rank," +
            "tea_class.t_id,tea_class.c_id from teacher t " +
            "left join tea_class on t.id = tea_class.t_id " +
            "left join class on tea_class.c_id = class.id where t.state !=2" +
            "<if test=\"cname!=null and cname !=''\">" +
            "and class.cname like #{cname}" +
            "</if>" +
            "<if test=\"name!=null and name !=''\">" +
            "and t.name like #{name}" +
            "</if>" +
            "</script>")
    List<Tclass> selectByIf(@Param("cname") String cname, @Param("name") String name);

    //添加教师信息
    @Insert("insert into teacher (number,name,subject,tel,position,u_id) values (#{number},#{name},#{subject},#{tel},#{position},#{u_id})")
    boolean addTeacher(@Param("u_id") Integer u_id, @Param("number") String number, @Param("name") String name, @Param("subject") String subject, @Param("tel") String tel, @Param("position") Integer position);

    //修改教师账号密码
    @Update("update user set password = #{newPassword} where id = #{u_id}")
    boolean changePsw(@Param("u_id") Integer u_id, @Param("newPassword") String newPassword);

    //查找非班主任教师
    @Select("select name from teacher where id = #{id} and position != 2")
    String findTeacher(int id);

    @Select("select t.id, t.number,t.name,t.subject,class.cname,class.rank ,"+
            "tea_class.t_id,tea_class.c_id " +
            "from teacher t left join tea_class on t.id = tea_class.t_id " +
            "left join class on tea_class.c_id = class.id where class.cname like #{cname}")
    List<Tclass> findClassInfo(String cname);

    //更改教师职位
    @Update("update teacher set position = 2 where name = #{name}")
    boolean addHead(String name);
}
